package tuillayer;
import java.util.InputMismatchException;
import java.util.Scanner;

import controllayer.*;

/**
 * Textbased UI which allows the user to input information about persons.
 */
public class DVDUI {

    // instance variables - object by the type DVDCtrl
    /** The dvd ctrl. */
    private DVDCtrl dvdCtrl;

    /** The instance. */
    private static DVDUI instance;

    /**
     * Constructor for DVDUI.
     */
    private DVDUI()
    {
        // Initialize dvdCtrl
        dvdCtrl = new DVDCtrl();
    }

    /**
     * Gets the single instance of DVDUI.
     *
     * @return single instance of DVDUI
     */
    public static DVDUI getInstance(){
        if(instance == null){
            instance = new DVDUI();
        }
        return instance;
    }

    /**
     * Dvd menu.
     */
    public void dvdMenu()
    {
        boolean exit = false;
        try {
            while(!exit)//that is: while exit is false
            {
                int choice = writedvdMenu(); 
                if(choice == 1)
                {   //Create dvd
                    System.out.println(dvdCtrl.createDVD(inputTitle(), inputBarcode(), inputPublicationDate()));
                }
                else if(choice == 2)
                { //find dvd
                    dvdCtrl.printDVD(inputTitle());
                } 
                else if(choice == 3)
                {
                    if(dvdCtrl.editDVD(inputTitle(), inputTitle(), inputBarcode(), inputPublicationDate()) == false)
                    {
                    	System.out.println("DVD not found!");
                    }
                    else
                    {
                    	System.out.println("DVD edited!");
                    }
                    	
                }
                else if(choice == 4)
                {
                    System.out.println(dvdCtrl.deleteDVD(inputTitle()));
                }
                else if(choice == 5)
                {
                    dvdCtrl.printAll();
                }
                else if(choice == 6)
                {
                	System.out.println(dvdCtrl.addCopy(inputTitle(), inputSerial(), inputPurchaseDate(), inputPurchasePrice()));
                }
                else if(choice == 7)
                {
                	System.out.println(dvdCtrl.deleteCopy(inputTitle(), inputSerial()));
                }
                else
                {
                    exit = true;
                }
                //end else
            }//end else

        }
        catch (InputMismatchException e) 
        {
            //do something clever with the exception
            System.out.println("Du skal indtaste et tal!");
            dvdMenu();
        }//end while
    }

    /**
     * Display DVD menu
     *
     * @return the int
     */
    public int writedvdMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** DVD Menu ***");
        System.out.println(" (1) Add a dvd");
        System.out.println(" (2) Find a dvd");
        System.out.println(" (3) Edit a dvd");
        System.out.println(" (4) Delete a dvd");
        System.out.println(" (5) Print all dvd's");
        System.out.println(" (6) Add a copy");
        System.out.println(" (7) Delete a copy");
        System.out.println(" (8) Exit");
        System.out.print("\n Choose: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    /**
     * Input serial.
     *
     * @return the string
     */
    public String inputSerial()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Serial:  ");
        String serial = keyboard.nextLine();

        return serial;
    }         

    /**
     * Input purchase date.
     *
     * @return the string
     */
    public String inputPurchaseDate()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Purchase Date: ");
        String purchaseDate = keyboard.nextLine();

        return purchaseDate;
    }

    /**
     * Input purchase price.
     *
     * @return the string
     */
    public String inputPurchasePrice()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Purchase Price: ");
        String purchasePrice = keyboard.nextLine();

        return purchasePrice;
    }

    /**
     * Input title.
     *
     * @return the string
     */
    public String inputTitle()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" DVD title:  ");
        String title = keyboard.nextLine();

        return title;
    }         

    /**
     * Input barcode.
     *
     * @return the string
     */
    public String inputBarcode()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" DVD barcode: ");
        String barcode = keyboard.nextLine();

        return barcode;
    }

    /**
     * Input publication date.
     *
     * @return the string
     */
    public String inputPublicationDate()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" DVD Publication Date (DD/MM/YYYY): ");
        String publicationDate = keyboard.nextLine();

        return publicationDate;
    }
}
